const cloud = require('wx-server-sdk')

cloud.init()

/// (id: UserId, credit: Int) => (StatusCode)
/// StatusCode:

const SUCCESS = 0;
const BAD_PARAM = -1;
const NOT_FOUND = -2;

exports.main = async (event) => {
  const id = event.id;
  const credit = event.credit;

  if (id == null || credit == null) {
    return {
      code: BAD_PARAM
    };
  }

  try {
    await cloud.database().collection('user').doc(id).update({
      data: {
        credit: credit
      }
    });
    return {
      code: SUCCESS
    };
  } catch (e) {
    return {
      code: NOT_FOUND,
      result: e
    };
  }
}